﻿using NUnit.Framework;
using Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.Models;
using System;
using System.IO;

namespace Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.Tests.Unit.Models
{
    [TestFixture]
    public class RuleSetConverterTest
    {
        private RuleSetConverter testee;


        [SetUp]
        public void SetUp()
        {
            testee = new RuleSetConverter(@"C:\tmp");
        }

        [Test]
        public void TestConvertWithNull()
        {
            Assert.IsNull(testee.Convert(null));
        }

        [Test]
        public void TestConvertWithEmpty()
        {
            Assert.IsNull(testee.Convert(""));
        }

        [Test]
        public void TestConvertWithBundledRuleSet()
        {
            var actual = testee.Convert(GetRuleSetPath("test.ruleset"));
            var expected = "Bundled;test.ruleset";
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void TestConvertWithCustomRuleSet()
        {
            var actual = testee.Convert(@"C:\tmp\test.ruleset");
            var expected = "Custom;test.ruleset";
            Assert.AreEqual(expected, actual);
        }

        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void TestConvertWithInvalidPath()
        {
            testee.Convert(@"C:\invalid\invalid.ruleset");
        }

        [Test]
        public void TestConvertWithWhiteSpace()
        {
            Assert.IsNull(testee.Convert(" "));
        }

        [Test]
        public void TestReverseWithNull()
        {
            Assert.IsNull(testee.Reverse(null));
        }

        [Test]
        public void TestReverseWithEmpty()
        {
            Assert.IsNull(testee.Reverse(""));
        }

        [Test]
        public void TestReverseWithWhiteSpace()
        {
            Assert.IsNull(testee.Reverse(" "));
        }

        [Test]
        public void TestReverseWithValueWithoutSemiColon()
        {
            Assert.IsNull(testee.Reverse("abc"));
        }

        [Test]
        public void TestReverseWithBundledRuleSet()
        {
            var actual = testee.Reverse("Bundled;test.ruleset");
            var expected = GetRuleSetPath("test.ruleset");
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void TestReverseWithCustomRuleSet()
        {
            var actual = testee.Reverse("Custom;test.ruleset");
            var expected = @"C:\tmp\test.ruleset";
            Assert.AreEqual(expected, actual);
        }

        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void TestReverseWithInvalidRuleSet()
        {
            testee.Reverse("RuleSet;test.ruleset");
        }

        private string GetRuleSetPath(string fileName)
        {
            return Path.Combine(Environment.GetEnvironmentVariable("VS100COMNTOOLS"), "..", "..", "Team Tools", "Static Analysis Tools", "Rule Sets", fileName);
        }

        private string GetRuleSetPath(string configurationPath, string ruleSetFileName)
        {
            return Path.Combine(Path.GetDirectoryName(configurationPath), ruleSetFileName);
        }
    }
}
